package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServcieImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //将数据插入数据库
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();

        setCheckGroupAndCheckItem(checkGroupId, checkitemIds);
       /*
       //对数组进行判空处理
       if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }*/
    }

    //查询分页数据
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> checkGroups = null;
        if (queryString != null && queryString.length() > 0) {
            checkGroups = checkGroupDao.selectByCondition("%" + queryString + "%");
        } else {
            checkGroups = checkGroupDao.selectByCondition(queryString);
        }
        return new PageResult(checkGroups.getTotal(), checkGroups.getResult());
    }

    //编辑数据回显
    @Override
    public CheckGroup findById(Integer id) {

        return checkGroupDao.findById(id);
    }

    //根据检查组查询包含的检查项信息
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {

        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    //编辑检查组信息保存至数据库
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //删除与该检查项相关联的信息
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //向中间表插入数据
        setCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);
        //将数据进行更新
        checkGroupDao.edit(checkGroup);
    }

    //查询所有的检查组信息
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findByAll();
    }

    //删除检查组
    @Override
    public void delete(Integer id) {
        Long count = checkGroupDao.selectCountByCheckGroupId(id);
        if (count > 0) {
            throw new RuntimeException("当前检查项组引用，无法删除!");
        }
        checkGroupDao.delete(id);
    }

    //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
